const axios = require('axios')
const iconv = require('iconv-lite')
const cheerio = require('cheerio')
const pool = require('../utils/utils.js')
const host = 'https://www.81book.com/'

module.exports = async (ctx, next) => {
  let bookList = await new Promise((resolve, reject) => {
    axios({
      method: 'get',
      url: host,
      responseType: 'stream'
    }).then(
      res => {
        let stream = [];
        res.data.on('data', st => {
          stream.push(st);
        });
        res.data.on('end', async () => {
          let buffer = Buffer.concat(stream);
          let domStr = iconv.decode(buffer, 'utf8');
          var $ = cheerio.load(domStr)
          var arr = []
          $('a').each(function () {
            var href = $(this).attr('href');
            // 筛选和去重
            if (href.slice(0, 6) === '/book/' && href.slice(-4) != 'html' && arr.indexOf(href) < 0) {
              arr.push(href)
            }
          });
          resolve(arr)
        })
      }
    ).catch(err => {
      console.log(err)
    })
  })
  console.log('bookList:',bookList)
  // bookList = ['/book/85148/']
  for (var i = 0; i < bookList.length; i++) {
    console.log(bookList[i])
    var book = {}
    axios({
      method: 'get',
      url: host + bookList[i],
      responseType: 'stream'
    }).then(res => {
      var stream = []
      res.data.on('data', st => {
        stream.push(st)
      })
      res.data.on('end', () => {
        let buffer = Buffer.concat(stream)
        let domStr = iconv.decode(buffer, 'utf8')
        console.log(domStr);
        var $ = cheerio.load(domStr)
        book.url = $('meta[property="og:url"]').attr('content')
        book.bookName = $('#info h1').text()
        book.author = $('#info p').eq(0).text().slice(7)
        book.intro = $('#intro p').eq(0).text()
        book.category = $('.con_top a').eq(1).text()
        book.status = $('#info p').eq(1).text().slice(7, 10)
        book.updateTime = $('#info p').eq(2).text().slice(5)
        book.lastChapterName = $('#info p').eq(3).find('a').text()
        book.lastChapterUrl = host + $('#info p').eq(3).find('a').attr('href')
        book.picUrl = host + $('#fmimg img').attr('src')
        var queryStr = `insert into ` + "`book`" + ` values ('${book.url}','${book.bookName}','${book.intro}','${book.category}','${book.status}','${book.author}','${book.lastChapterName}','${book.lastChapterUrl}','${book.picUrl}','${book.updateTime}',${0},0)`;
        // console.log(queryStr)
        pool.query(queryStr, (err, result) => {
          if (err) {
            console.log('存储失败' + err)
          } else {
            console.log('存储成功')
          }
        })
      })
    })
  }

  ctx.body = {
    code: 200,
    msg: '成功'
  }

}
